package jnpf.engine.util;

import cn.hutool.core.collection.CollUtil;
import jnpf.engine.model.InterfaceInfoModel;
import jnpf.engine.model.flowbefore.FlowTemplateAllModel;
import jnpf.engine.model.flowengine.FlowModel;
import jnpf.engine.model.flowmessage.FlowParameterModel;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事件数据添加
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2022/8/20 8:49
 */
public class FlowContextHolder {
    private static final ThreadLocal<InterfaceInfoModel> INTERFACE_INPUT_STR = new ThreadLocal<>();
    private static final ThreadLocal<List<FlowParameterModel>> CONTEXT_DB_NAME_HOLDER = new ThreadLocal<>();
    /**
     * 用这个存储每次变更的情况
     * key  业务表逐渐
     * v 业务表数据
     */
    private static final ThreadLocal<Map<String, Map<String, Object>>> CONTEXT_DATA = new ThreadLocal<>();

    private static final ThreadLocal<Map<String, Map<String, Object>>> CHILD_DATA = new ThreadLocal<>();

    private static final ThreadLocal<Map<String, List<Map<String, Object>>>> FORM_OPERATES_DATA = new ThreadLocal<>();

    private static final ThreadLocal<Map<String,String>>  NODE_USERS=new ThreadLocal<>();

    private static final ThreadLocal<Map<String, FlowTemplateAllModel>>  TEMPLATE_JSON_ENTITY_CACHE=new ThreadLocal<>();
    private static final ThreadLocal<Map<String, Map<String,Object>>>  FORM_DATA_CACHE=new ThreadLocal<>();
    private static final ThreadLocal<List<String>> ADD_MULTI_NODE_USER_IDS =new ThreadLocal<>();

    private static final ThreadLocal<List<String>> PROCESS_INSTANCE_IDS=new ThreadLocal<>();
    private static final ThreadLocal<FlowModel> FLOW_MODEL=new ThreadLocal<>();


    public  static void addInterfaceModel(InterfaceInfoModel flowModel){
        INTERFACE_INPUT_STR.set(flowModel);
    }
    public  static InterfaceInfoModel getInterfaceModel(){
        return INTERFACE_INPUT_STR.get();
    }

    public static void removeInterfaceModel(){
        INTERFACE_INPUT_STR.remove();
    }



    public  static void addFlowModel(FlowModel flowModel){
        FLOW_MODEL.set(flowModel);
    }
    public  static FlowModel getFlowModel(){
        return FLOW_MODEL.get();
    }

    public static void removeFlowModel(){
        FLOW_MODEL.remove();
    }


    public  static void addProcessInstanceIds(List<String> processInstanceIds){
        List<String> list = PROCESS_INSTANCE_IDS.get();
        if(CollUtil.isNotEmpty(list)){
            list.addAll(processInstanceIds);
        }
        else{
            list=new ArrayList<>();
            list.addAll(processInstanceIds);
        }
        PROCESS_INSTANCE_IDS.set(list);
    }
    public  static List<String> getProcessInstanceIds(){
        return PROCESS_INSTANCE_IDS.get();
    }

    public static void removeProcessInstanceIds(){
        PROCESS_INSTANCE_IDS.remove();
    }

    public  static void addMultiNodeUserIds(List<String> copyIds){
        ADD_MULTI_NODE_USER_IDS.set(copyIds);
    }
    public  static List<String> getMultiNodeUserIds(){
        return ADD_MULTI_NODE_USER_IDS.get();
    }

    public static void removeMultiNodeUserIds(){
        ADD_MULTI_NODE_USER_IDS.remove();
    }

    /**
     * 添加当前事件对象
     */
    static void addEvent(String interId, Map<String, String> parameterMap) {
        FlowParameterModel model = new FlowParameterModel();
        model.setInterId(interId);
        model.setParameterMap(parameterMap);
        List<FlowParameterModel> list = CONTEXT_DB_NAME_HOLDER.get() != null ? CONTEXT_DB_NAME_HOLDER.get() : new ArrayList<>();
        list.add(model);
        CONTEXT_DB_NAME_HOLDER.set(list);
    }
    public static void addNodeFormData(String nodeCode,Map<String,Object> formData){
        Map<String, Map<String,Object>> nodeFormData=FORM_DATA_CACHE.get()!=null?FORM_DATA_CACHE.get():new HashMap<>();
        nodeFormData.put(nodeCode,formData);
        FORM_DATA_CACHE.set(nodeFormData);
    }
    public static Map<String, Map<String,Object>> getNodeFormData(){
        return FORM_DATA_CACHE.get()!=null?FORM_DATA_CACHE.get():new HashMap<>();
    }
    public static void removeNodeFormData(){
        FORM_DATA_CACHE.remove();
    }

    public static void addNodeUser(String nodeCode,String nodeUser){
        Map<String,String> nodeUsers=NODE_USERS.get()!=null?NODE_USERS.get():new HashMap<>();
        nodeUsers.put(nodeCode,nodeUser);
        NODE_USERS.set(nodeUsers);
    }
    public static Map<String,String> getNodeUser(){
        return NODE_USERS.get()!=null?NODE_USERS.get():new HashMap<>();
    }
    public static void removeNodeUser(){
        NODE_USERS.remove();
    }

    public static void addTemplateJson(String nodeCode,FlowTemplateAllModel FlowTemplateAllModel){
        Map<String,FlowTemplateAllModel> nodeUsers=TEMPLATE_JSON_ENTITY_CACHE.get()!=null?TEMPLATE_JSON_ENTITY_CACHE.get():new HashMap<>();
        nodeUsers.put(nodeCode,FlowTemplateAllModel);
        TEMPLATE_JSON_ENTITY_CACHE.set(nodeUsers);
    }
    public static Map<String,FlowTemplateAllModel> getTemplateJson(){
        return TEMPLATE_JSON_ENTITY_CACHE.get()!=null?TEMPLATE_JSON_ENTITY_CACHE.get():new HashMap<>();
    }
    public static void removeTemplateJson(){
        TEMPLATE_JSON_ENTITY_CACHE.remove();
    }


    /**
     * 获取当前事件对象
     */
    public static List<FlowParameterModel> getAllEvent() {
        return CONTEXT_DB_NAME_HOLDER.get() != null ? CONTEXT_DB_NAME_HOLDER.get() : new ArrayList<>();
    }

    /**
     * 添加数据
     */
    public static void addData(String formId, Map<String, Object> parameterMap) {
        if (StringUtil.isNotEmpty(formId) && getAllEvent().size() == 0) {
            Map<String, Map<String, Object>> map = CONTEXT_DATA.get() != null ? CONTEXT_DATA.get() : new HashMap<>();
            map.put(formId, JsonUtil.entityToMap(parameterMap));
            CONTEXT_DATA.set(map);
        }
    }

    /**
     * 获取数据
     */
    static Map<String, Map<String, Object>> getAllData() {
        Map<String, Map<String, Object>> data = CONTEXT_DATA.get() != null ? CONTEXT_DATA.get() : new HashMap<>();
        return data;
    }

    /**
     * 清除数据
     */
    public static void clearAll() {
        CONTEXT_DB_NAME_HOLDER.remove();
        CONTEXT_DATA.remove();
        CHILD_DATA.remove();
        FORM_OPERATES_DATA.remove();
    }


    /**
     * 添加数据
     */
    public static void addChildData(String taskId, String formId, Map<String, Object> parameterMap) {
        if (StringUtil.isNotEmpty(taskId) && StringUtil.isNotEmpty(formId) && getAllEvent().size() == 0) {
            Map<String, Map<String, Object>> map = CHILD_DATA.get() != null ? CHILD_DATA.get() : new HashMap<>();
            map.put(taskId + "_jnpf_" + formId, JsonUtil.entityToMap(parameterMap));
            CHILD_DATA.set(map);
        }
    }

    /**
     * 获取数据
     */
    public static Map<String, Map<String, Object>> getChildAllData() {
        Map<String, Map<String, Object>> data = CHILD_DATA.get() != null ? CHILD_DATA.get() : new HashMap<>();
        return data;
    }

    /**
     * 获取权限
     */
    public static Map<String, List<Map<String, Object>>> getFormOperates() {
        Map<String, List<Map<String, Object>>> data = FORM_OPERATES_DATA.get() != null ? FORM_OPERATES_DATA.get() : new HashMap<>();
        return data;
    }

    /**
     * 添加权限
     */
    public static void addFormOperates(String taskId, String formId, List<Map<String, Object>> formOperates) {
        if (StringUtil.isNotEmpty(taskId) && StringUtil.isNotEmpty(formId) && getAllEvent().size() == 0) {
            Map<String, List<Map<String, Object>>> map = FORM_OPERATES_DATA.get() != null ? FORM_OPERATES_DATA.get() : new HashMap<>();
            if (map.get(taskId + "_jnpf_" + formId) == null) {
                map.put(taskId + "_jnpf_" + formId, formOperates);
                FORM_OPERATES_DATA.set(map);
            }
        }
    }

}
